from Case.python_ui.Common.basepage import Basepage
import time,pytest

from Case.python_ui.PageLocate.login_locator import Loginlocator
from Case.python_ui.PageLocate.user_all_notification_locator import UserAllNotificationLocator
from Case.python_ui.Test_data.common_data import Commondata
from Case.python_ui.Test_data.login_data import Logindata


class UserAllNotificationPage(Basepage):


    #进入AllNotification菜单页面
    def enter_all_notification_page(self):
        time.sleep(2)
        doc = "进入AllNotification菜单页面"
        #print(doc)
        for i in range(3):
            try:
                self.wait_elevisble(UserAllNotificationLocator.notification_tab,doc)
                self.click_element(UserAllNotificationLocator.notification_tab,doc)
                self.wait_elevisble(UserAllNotificationLocator.all_notification_tab,doc)
                self.click_element(UserAllNotificationLocator.all_notification_tab,doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue

    # TL597157 | 页面刷新功能验
    def refresh_page(self):
        self.enter_all_notification_page()
        time.sleep(2)
        doc = "页面刷新功能验"
        self.wait_elevisble(UserAllNotificationLocator.total_count,doc)
        total = self.get_element(UserAllNotificationLocator.total_count,doc).text
        self.wait_elevisble(UserAllNotificationLocator.refresh_button,doc)
        self.click_element(UserAllNotificationLocator.refresh_button,doc)
        time.sleep(2)
        total_new = self.get_element(UserAllNotificationLocator.total_count,doc).text
        assert total == total_new

    # TL597151 | All Notification列表页面检查
    def check_page(self):
        self.enter_all_notification_page()
        time.sleep(2)
        doc = "All Notification列表页面检查"
        self.wait_elevisble(UserAllNotificationLocator.all_notification,doc)
        self.click_element(UserAllNotificationLocator.all_notification,doc)
        self.wait_elevisble(UserAllNotificationLocator.announcement, doc)
        self.click_element(UserAllNotificationLocator.announcement, doc)
        self.wait_elevisble(UserAllNotificationLocator.system, doc)
        self.click_element(UserAllNotificationLocator.system, doc)
        self.wait_elevisble(UserAllNotificationLocator.product, doc)
        self.click_element(UserAllNotificationLocator.product, doc)
        self.wait_elevisble(UserAllNotificationLocator.security, doc)
        self.click_element(UserAllNotificationLocator.security, doc)
        self.wait_elevisble(UserAllNotificationLocator.promotion, doc)
        self.click_element(UserAllNotificationLocator.promotion, doc)

    # TL597154 | 未读消息显示验证
    def check_new_message(self):
        self.enter_all_notification_page()
        time.sleep(2)
        doc = "未读消息显示验证"
        #校验右上角图标
        #红点
        self.wait_elevisble(UserAllNotificationLocator.new_dot_in_icon,doc)
        #new标签
        self.wait_elevisble(UserAllNotificationLocator.message_icon,doc)
        self.click_element(UserAllNotificationLocator.message_icon,doc)
        time.sleep(2)
        self.wait_elevisble(UserAllNotificationLocator.new_tag_in_icon,doc)
        #校验列表红点
        self.wait_elevisble(UserAllNotificationLocator.new_dot_in_list,doc)
        #校验菜单标签
        self.wait_elevisble(UserAllNotificationLocator.new_tag_in_menu,doc)

    def view_notification_detail(self):
        self.enter_all_notification_page()
        time.sleep(2)
        doc = "User 查看消息详情页"
        self.wait_elevisble(UserAllNotificationLocator.first_message_title,doc)
        title = self.get_element(UserAllNotificationLocator.first_message_title,doc).text
        self.wait_elevisble(UserAllNotificationLocator.first_message_title_button,doc)
        self.click_element(UserAllNotificationLocator.first_message_title_button,doc)
        time.sleep(2)
        self.wait_elevisble(UserAllNotificationLocator.message_detail_title,doc)
        title2 = self.get_element(UserAllNotificationLocator.message_detail_title,doc).text
        assert title == title2

    def flip_notification_detail_page(self):
        self.enter_all_notification_page()
        time.sleep(2)
        doc = "User 站内信查看页面优化"
        self.wait_elevisble(UserAllNotificationLocator.total_count,doc)
        result = self.get_element(UserAllNotificationLocator.total_count, doc).text
        result = result.split(" ")
        result = int(result[1], base=10)
        if(result > 0):
            self.wait_elevisble(UserAllNotificationLocator.first_message_title_button, doc)
            self.click_element(UserAllNotificationLocator.first_message_title_button, doc)
            time.sleep(2)
            self.wait_elevisble(UserAllNotificationLocator.previous_button,doc)
            is_disable = self.get_element(UserAllNotificationLocator.previous_button,doc).get_attribute("disabled")
            assert is_disable == "true"
            self.wait_elevisble(UserAllNotificationLocator.close_message_detail, doc)
            self.click_element(UserAllNotificationLocator.close_message_detail, doc)

        if(result > 2):
            self.wait_elevisble(UserAllNotificationLocator.second_message_title_button, doc)
            self.click_element(UserAllNotificationLocator.second_message_title_button, doc)
            time.sleep(2)
            self.wait_elevisble(UserAllNotificationLocator.previous_button, doc)
            is_disable = self.get_element(UserAllNotificationLocator.previous_button, doc).get_attribute("disabled")
            print('>>>>>>>>>>>>>>>>>>>>isdisable1:',is_disable)
            assert is_disable == None
            self.wait_elevisble(UserAllNotificationLocator.close_message_detail, doc)
            is_disable = self.get_element(UserAllNotificationLocator.previous_button, doc).get_attribute("disabled")
            assert is_disable == None


    def send_emai_verification_code(self, cmdopt):
        self.driver.get(Commondata.url[cmdopt]['user'])
        time.sleep(10)
        doc = "发送一个登录的验证码"
        self.wait_elevisble(Loginlocator.login_username_ele, doc)
        self.wait_elevisble(Loginlocator.login_password_ele, doc)
        time.sleep(1)
        self.send_keys(Loginlocator.login_username_ele, Logindata.user_data[cmdopt]['user']['username'], doc)
        self.send_keys(Loginlocator.login_password_ele, Logindata.user_data[cmdopt]['user']['password'], doc)
        time.sleep(1)
        self.click_element(Loginlocator.login_submit_ele, doc)
        time.sleep(1)
        # self.wait_elevisble(UserAllNotificationLocator.send_code_button,doc)
        # self.click_element(UserAllNotificationLocator.send_code_button, doc)

    def mark_as_read_in_list(self):
        self.enter_all_notification_page()
        time.sleep(2)
        doc = "User 在列表处将消息标记为已读"
        self.wait_elevisble(UserAllNotificationLocator.list_first_record,doc)
        self.wait_elevisble(UserAllNotificationLocator.list_select_first_button,doc)
        self.click_element(UserAllNotificationLocator.list_select_first_button,doc)
        time.sleep(1)
        class1 = self.get_element(UserAllNotificationLocator.list_first_record,doc).get_attribute("class")
        if(class1 == "antd4-table-row antd4-table-row-level-0 antd4-table-row-selected"):
            self.wait_elevisble(UserAllNotificationLocator.list_mark_as_read,doc)
            self.click_element(UserAllNotificationLocator.list_mark_as_read,doc)
        else:
            print("Error：列表处未选中消息")
        time.sleep(3)
        class2 = self.get_element(UserAllNotificationLocator.list_first_record, doc).get_attribute("class")
        if "antd4-table-row-selected" in class2:
            print("标记已读未成功")
            assert 1 == 0
        else:
            assert 1 == 1

    def delete_notification_in_list(self):
        self.enter_all_notification_page()
        time.sleep(2)
        doc = "User 在列表处删除通知"
        self.wait_elevisble(UserAllNotificationLocator.total_count,doc)
        total_count = self.get_element(UserAllNotificationLocator.total_count,doc).text
        total_count = total_count.split(" ")
        total_count = int(total_count[1],base=10)
        # print(">>>>>>>>>>>>>>>total_count:",total_count)
        self.wait_elevisble(UserAllNotificationLocator.list_delete_button, doc)
        self.click_element(UserAllNotificationLocator.list_delete_button, doc)
        self.wait_elevisble(UserAllNotificationLocator.delete_confirm_button,doc)
        self.click_element(UserAllNotificationLocator.delete_confirm_button,doc)
        time.sleep(2)
        self.wait_elevisble(UserAllNotificationLocator.total_count, doc)
        result = self.get_element(UserAllNotificationLocator.total_count, doc).text
        result = result.split(" ")
        result = int(result[1],base=10)
        assert total_count == result + 1

    def view_all_notification(self):
        self.enter_all_notification_page()
        time.sleep(2)
        doc = "用户查看所有消息通知"
        self.wait_elevisble(UserAllNotificationLocator.total_count,doc)
        total_count = self.get_element(UserAllNotificationLocator.total_count, doc).text
        total_count = total_count.split(" ")
        total_count = int(total_count[1], base=10)
        self.wait_elevisble(UserAllNotificationLocator.announcement, doc)
        self.click_element(UserAllNotificationLocator.announcement, doc)

        self.wait_elevisble(UserAllNotificationLocator.system, doc)
        self.click_element(UserAllNotificationLocator.system, doc)
        self.wait_elevisble(UserAllNotificationLocator.product, doc)
        self.click_element(UserAllNotificationLocator.product, doc)
        self.wait_elevisble(UserAllNotificationLocator.security, doc)
        self.click_element(UserAllNotificationLocator.security, doc)
        self.wait_elevisble(UserAllNotificationLocator.promotion, doc)
        self.click_element(UserAllNotificationLocator.promotion, doc)
